﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.VehicleController
{
    public class VehicleLodComponent : MonoBehaviour
    {
        [SerializeField]
        private int _lod = 0;

        public int LOD
        {
            set
            {
                if (value != _lod)
                {
                    _lod = value;
                    RefreshLOD();
                }
            }
        }
        
        public List<MeshRenderer> lod0MeshRenderers;
        public float lod1Distance = 20;
        public List<MeshRenderer> lod1MeshRenderers;
        public float lod2Distance = 55;
        public List<MeshRenderer> lod2MeshRenderers;

        private void Awake()
        {
            RefreshLOD();
        }

        private void OnEnable()
        {
            
        }

        public void RefreshLOD()
        {
            if (_lod == 0)
            {
                SwitchMeshRenderers(lod1MeshRenderers, false);
                SwitchMeshRenderers(lod2MeshRenderers, false);
                SwitchMeshRenderers(lod0MeshRenderers, true);
            }
            else if (_lod == 1)
            {
                SwitchMeshRenderers(lod0MeshRenderers, false);
                SwitchMeshRenderers(lod2MeshRenderers, false);
                SwitchMeshRenderers(lod1MeshRenderers, true);
            }
            else
            {
                SwitchMeshRenderers(lod0MeshRenderers, false);
                SwitchMeshRenderers(lod1MeshRenderers, false);
                SwitchMeshRenderers(lod2MeshRenderers, true);
            }
        }

        public void SwitchMeshRenderers(List<MeshRenderer> meshRenderers,bool enable)
        {
            if (meshRenderers == null) return;
            foreach (var meshRenderer in meshRenderers)
            {
                if (meshRenderer && meshRenderer.enabled != enable)
                {
                    meshRenderer.enabled = enable;
                }
            }
        }
    }
}